//
//  HNNewfeatureViewController.m
//  HappyNews
//
//  Created by baiping on 16/1/9.
//  Copyright © 2016年 BPWBANP. All rights reserved.
//

#import "HNNewfeatureViewController.h"
#define krigthMargin 100
#define kAnimationWidth 80
#import "HNRootTabBarViewController.h"


@interface HNNewfeatureViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) UILabel *leftFirLabel;

@property (nonatomic, strong) UILabel *leftSecLabel;

@property (nonatomic, strong) UILabel *leftThiLabel;

@property (nonatomic, strong) UILabel *leftFouLabel;

@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation HNNewfeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self addMainScrollView];
    
    [self addMainScrollViewImage];
    
    [self addLeftAnimationSubviews];
}

- (void)addMainScrollView{
    
    _mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    
    _mainScrollView.bounces = NO;
    
    _mainScrollView.pagingEnabled = YES;
    
    [self.view addSubview:_mainScrollView];
    
    _mainScrollView.contentSize = CGSizeMake(kScreenWidth * 3, kScreenHeight);
    
    _mainScrollView.showsHorizontalScrollIndicator = NO;
    
    _mainScrollView.showsVerticalScrollIndicator = NO;
    
    _mainScrollView.delegate = self;
    
    // 添加分页
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 30)];
    
    [self.view addSubview:_pageControl];
    
    _pageControl.tintColor = [UIColor redColor];
    
    _pageControl.numberOfPages = 3;
    
    
}

- (void)addMainScrollViewImage{
    
    UIImageView *firstIcon = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    
    firstIcon.image = [UIImage imageNamed:@"HN_NewFeature_left@2x"];
    
    [_mainScrollView addSubview:firstIcon];
    
    
    UIImageView *secondIcon = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight)];
    
    secondIcon.image = [UIImage imageNamed:@"HN_NewFeature_center@2x"];
    
    [_mainScrollView addSubview:secondIcon];
    
    
    UIImageView *thirdIcon = [[UIImageView alloc]initWithFrame:CGRectMake(2 * kScreenWidth, 0, kScreenWidth, kScreenHeight)];
    
    thirdIcon.image = [UIImage imageNamed:@"HN_NewFeature_right@2x"];
    
    [_mainScrollView addSubview:thirdIcon];
    
    thirdIcon.userInteractionEnabled = YES;
    
    // 给第三个页面添加button
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    button.frame = CGRectMake(100, _pageControl.top - 250, kScreenWidth - 200, 60);
    
    button.layer.cornerRadius = 20;
    
    button.layer.masksToBounds = YES;
    
    [button setTitle:@"开启快乐" forState:(UIControlStateNormal)];
    
    [button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    
    button.titleLabel.font = [UIFont systemFontOfSize:20];
    
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [thirdIcon addSubview:button];
    
    
    
}

// 添加动画所需要控件

- (void)addLeftAnimationSubviews{
    
    // 左视图动画控件
    
    _leftFirLabel = [[UILabel alloc]init];
    
    [_mainScrollView addSubview:_leftFirLabel];
    
    
    _leftSecLabel = [[UILabel alloc]init];
    
    [_mainScrollView addSubview:_leftSecLabel];
    
    _leftThiLabel = [[UILabel alloc]init];
    
    [_mainScrollView addSubview:_leftThiLabel];
    
    _leftFouLabel = [[UILabel alloc]init];
    
    [_mainScrollView addSubview:_leftFouLabel];
    
    _leftFirLabel.frame = CGRectMake(kScreenWidth * 0.3, kScreenHeight * 0.7, kAnimationWidth, kAnimationWidth);
    
    _leftFirLabel.alpha = 1.0;
    
    _leftSecLabel.frame = CGRectMake(kScreenWidth * 0.6, kScreenHeight * 0.2,kAnimationWidth, kAnimationWidth);
    
    _leftFirLabel.text = @"skkdfsjkdfhkshfksf";
    
    _leftSecLabel.alpha = 0;
    
    _leftSecLabel.text = @"sdkhfs上啦;dfjskihf";
    

}

- (void)buttonAction:(UIButton *)sender{
    
    // 进行主界面 并且把它作为weindow的根视图控制器
    [UIApplication sharedApplication].statusBarHidden = NO;
    
    [UIApplication sharedApplication].keyWindow.rootViewController = [[HNRootTabBarViewController alloc] init];
    
    NSLog(@"点击了按钮");
    
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
  
    
    CGFloat contentSizeX = _mainScrollView.contentOffset.x;
    
    if (contentSizeX <= kScreenWidth) {
        
        _leftFirLabel.alpha = 1.0 - contentSizeX / kScreenWidth * 1.5;
        
        _leftSecLabel.alpha = contentSizeX / kScreenWidth;

    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat contentSizeX = _mainScrollView.contentOffset.x;
    
    _pageControl.currentPage = contentSizeX / kScreenWidth;
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


@end
